package club.sanmubird.springboot_mysqlAndMongodb.mongodb.dao;

import club.sanmubird.springboot_mysqlAndMongodb.mongodb.entity.Student;
import com.mongodb.WriteResult;
import org.bson.types.ObjectId;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Key;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.mongodb.morphia.query.UpdateResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @program: springboot-simpleMongDb
 * @description: StudentDao
 * @author: sam
 * @create: 2018-07-18 23:27
 * @update: 2018-07-18 23:27
 * @update: Sam
 **/
@Repository
public class StudentDao {

	@Autowired
	private Datastore datastore;

	public Key<Student> create(Student student) {
		return datastore.save(student);
	}

	public Student read(ObjectId id) {
		return datastore.get(Student.class, id);
	}

	public UpdateResults update(Student student, UpdateOperations<Student> operations) {
		return datastore.update(student, operations);
	}

	public WriteResult delete(Student student) {
		return datastore.delete(student);
	}

	public UpdateOperations<Student> createOperations() {
		return datastore.createUpdateOperations(Student.class);
	}

	public List<Student> getAllStudents() {
		Query<Student> query = datastore.createQuery(Student.class);
		return query.asList();
	}
}
